﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Experiements.WebTechs2013.Service;

namespace Experiements.WebTechs2013.WebApi.Controllers
{
    
    public class ProductsApiController : ApiController
    {
        private ProductsService _Service;

        public ProductsApiController()
        {
            _Service = new ProductsService();
        }

        // GET api/values
        public IEnumerable<Product> Get()
        {
            var products = _Service.GetProducts();
            return products;
        }

        // GET api/values/5
        public Product Get(Guid id)
        {
            var product = _Service.GetProduct(id);
            return product;
        }

        // POST api/values
        //public void Post([FromBody]IEnumerable<Product> products)
        //{
          

        //    _Service.SetProducts(products);
        //}

        public HttpResponseMessage Post([FromBody]Product prod)
        {
            _Service.SetProducts(new[] { prod });

            var response = Request.CreateResponse<Product>(HttpStatusCode.Created, prod);
            string uri = Url.Link("WithActionAPI", new { id = prod.Id });
            response.Headers.Location = new Uri(uri);
            return response;
        }

        // PUT api/values
        [HttpPut]
        public void Sync([FromBody]IEnumerable<Product> products)
        {
            //if (!this.User.IsInRole("Author"))
            //    return;

            _Service.SetProducts(products);
           
        }

        // PUT api/values/5
        public void Put(Guid id, [FromBody]Product product)
        {
            //if (!this.User.IsInRole("Author"))
            //    return;

            _Service.SetProduct(product);
        }

        // DELETE api/values/5
      
        public void Delete(Guid id)
        {
            //if (!this.User.IsInRole("Author"))
            //    return;

            _Service.RemoveProduct(id);
        }
    }
}
